package com.flowerSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flowerSystem.entity.*;
import com.flowerSystem.pojo.FenYe;
import com.flowerSystem.service.OrderlistService;
import com.flowerSystem.util.PageResult;
import com.flowerSystem.util.ResponseResult;
import com.flowerSystem.vo.OrderListVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/manager")
@CrossOrigin("*")
@Slf4j
public class OrderlistController {
    @Resource
    private OrderlistService orderlistService;



    /**
     * 管理端查询所有订单信息
     * 页面刷新时，显示所有订单信息，分页
     * @param
     * @return
     */
    @PostMapping("/orderAllManager")
    public ResponseResult orderManagerAll(@RequestBody FenYe fenYe){
        PageResult pageResult = orderlistService.orderManagerAll(fenYe.getPageNum(), fenYe.getPageSize());
        if(pageResult != null && pageResult.getTotal() > 0){
            log.info("ok...");
            return ResponseResult.ok(pageResult);
        }
        log.info("没有查到任何订单信息...");
        return ResponseResult.fail();
    }


    /**
     * 管理员查询所有 已支付 的订单信息，加分页（管理端用来 接单 使用）
     * @return
     */
    @GetMapping("/orderAllManagerByPrepaid")
    public ResponseResult productInformPrepaid(@RequestBody FenYe fenYe){
        IPage<OrderListManager> iPage = orderlistService.productInformPrepaid(new Page<>(fenYe.getPageNum(), fenYe.getPageSize()));
        if(iPage != null && iPage.getTotal() > 0){
            log.info("查询到订单数据...");
            System.out.println("总页数："+iPage.getPages());
            System.out.println("总记录数："+iPage.getTotal());
            iPage.getRecords().forEach(System.out::println);
            return ResponseResult.ok(iPage);
        }
        log.info("没有查询当相关的订单数据...");
        return ResponseResult.fail();
    }


    /**
     * 管理员查询所有 已完成 的订单信息（客户端已确认收货，管理员查询所有已完成的历史订单信息）
     * @param
     * @return
     */
    @PostMapping("/orderAllManagerByFinish")
    public ResponseResult productInformFinish(@RequestBody FenYe fenYe){
        log.info("进入...");
        IPage<OrderListManager> iPage = orderlistService.productInformFinish(new Page<>(fenYe.getPageNum(), fenYe.getPageSize()));
        if(iPage != null && iPage.getTotal() > 0){
            log.info("查询到订单数据...");
            System.out.println("总页数："+iPage.getPages());
            System.out.println("总记录数："+iPage.getTotal());
            iPage.getRecords().forEach(System.out::println);
            return ResponseResult.ok(iPage);
        }
        log.info("没有查询当相关的订单数据...");
        return ResponseResult.fail();
    }



    /**
     * 通过订单编号查询对应的订单信息(精准查询)
     * @param
     * @return
     */
    @GetMapping("/orderListFinishLike")
    public ResponseResult orderListFinishLike(String orderNumber){
        OrderListManager orderListManager = orderlistService.orderListFinishLike(orderNumber);
        if(orderListManager != null){
            return ResponseResult.ok(orderListManager);
        }
        return ResponseResult.fail();
    }

    /**
     * 当管理端点击 接单 按钮的时候，将 order_status 的状态值 已支付 修改为 已接单
     * @param
     * @return
     */
    @PutMapping("/orderAllManagerByTake/{id}")
    public ResponseResult productInformTake(@PathVariable int id){
        log.info("进入...");
        int rows = orderlistService.productInformTake(id);
        if(rows > 0){
            return ResponseResult.ok(null);
        }
        return ResponseResult.fail();
    }


    /**
     * 当管理端点击 派送 按钮的时候，将 order_status 的状态值 已接单 修改为 已派送
     * @param id
     * @return
     */
    @PutMapping("/orderAllManagerByDelivery/{id}")
    public ResponseResult productInformDelivery(@PathVariable("id") int id){
        log.info("进入...");
        int rows = orderlistService.productInformDelivery(id);
        if(rows > 0){
            return ResponseResult.ok(null);
        }
        return ResponseResult.fail();
    }

//    ------------------------------------------------------------------------------------------------------
    @PostMapping("/order")
    public ResponseResult getOrderList(@RequestBody Order order){
        Order orderNow = orderlistService.getOrderList(order);
            return ResponseResult.ok(orderNow);
    }

    //取消订单
    @DeleteMapping("/order")
    public ResponseResult deleteOrderList(@RequestBody String orderNumber){
        int i =orderlistService.deleteOrderList(orderNumber);
        if (i!=0){
            return ResponseResult.ok(null);
        }
        return ResponseResult.fail();
    }
    //修改订单
    @PutMapping("/order")
    public ResponseResult updateOrderList(@RequestBody Orderlist orderlist){
        orderlistService.updateOrderList(orderlist);
        return null;
    }

    //查看订单
    @GetMapping("/order")
    public ResponseResult getOrderListByUser(int userId){
        List<OrderPermition> orderPermition =orderlistService.getOrderListbyUser(userId);
        return ResponseResult.ok(orderPermition);
    }


    //用户点击接收物品
    @GetMapping("/order/ok")
    public ResponseResult setOrderListTime(String orderNumber){
        orderlistService.setOrderListTime(orderNumber);
        return ResponseResult.ok(null);
    }
    //用户根据订单编号查询订单
    @GetMapping("/order/orderNumber")
    public ResponseResult getOrderByOrderNumber(String orderNumber){
        OrderPermition o  =orderlistService.getOrderByOrderNumber(orderNumber);
        System.out.println("o = " + o);
        return ResponseResult.ok(orderlistService.getOrderByOrderNumber(orderNumber));
    }

}
